package com.dhcc.DRGsHosp.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.HospInfoFacade;
import com.dhcc.DRGsHosp.facade.dto.HospInfoDTO;


@Controller("drgsControllerFuLianxia20180118")
@RequestMapping("/HospInfo")
public class HospInfoController {
		
	@Autowired
	private HospInfoFacade hospInfoFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(HospInfoDTO hospInfoDTO) {
		return hospInfoFacade.creatHospInfo(hospInfoDTO);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public InvokeResult update(HospInfoDTO hospInfoDTO) {
		return hospInfoFacade.updateHospInfo(hospInfoDTO);
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(HospInfoDTO hospInfoDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<HospInfoDTO> all = hospInfoFacade.pageQueryHospInfo(hospInfoDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult remove(@RequestParam String hospCodes) {
		String[] value = hospCodes.split(",");
        String[] idArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        					idArrs[i] = value[i];
			        				        }
        return hospInfoFacade.removeHospInfos(idArrs);
	}
	
    @InitBinder    
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
        dateFormat.setLenient(false);    
        //CustomDateEditor 可以换成自己定义的编辑器。
        //注册一个Date 类型的绑定器 。
        binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
    
    /**
     *  
     *  isHospOrderExist.  
     *  @Description：院区编码是否存在
     *  @Author fulianxia
     *  @param hospOrder
     *  @return 
     *  @Date:2018年3月26日
     */
    @ResponseBody
    @RequestMapping("/isHospOrderExist")
    public boolean isHospOrderExist(String hospOrder,String hospCode) {
    	return  hospInfoFacade.isHospOrderExist(hospOrder, hospCode);
    }
}
